package com.roc.dao.impl;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.NonUniqueResultException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.agent.IAgentInfoDao;
import com.roc.pojo.Operators;
import com.roc.pojo.ProfitPercent;
import com.roc.pojo.ProfitRule;
import com.roc.util.constant.TblBlackInfType;

/**
 * @author chenkai
 * @date 2014-08-10
 * 代理商操作
 * */
@Repository
public class AgentInfoDaoImpl extends BaseDaoImpl implements IAgentInfoDao {

	@Override
	public List<ProfitRule> queryProfitRule(String id) throws DaoException {
		List<ProfitRule> profitRuleList=new LinkedList<ProfitRule>();
		try {
			Session session = sessionFactory.getCurrentSession();
			StringBuffer sb=new StringBuffer("from ProfitRule p where p.agentId = ? order by p.mccTypeCode");
			
			Query query=session.createQuery(sb.toString());
			query.setString(0, id);

			profitRuleList=query.list();
		}catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询代理商分润规则出错");
		}
		return profitRuleList;
	}

	@Override
	public Operators queryOperatorInfo(String accountNo) throws DaoException {
		Operators result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from Operators m where m.accountNo = :accountNo");
			query.setString("accountNo", accountNo);
			result = (Operators) query.uniqueResult();
		}catch(NonUniqueResultException nure){
			logger.debug("查询Operators不唯一", nure);
			throw new DaoException("查询代理商不唯一");
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询出错");
		}
		return result;
	}
	
	public Operators loadOperatorsById(Long id) throws DaoException {
		Operators result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			result = (Operators) session.get(Operators.class, id);
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询出错");
		}
		return result;
	}

	@Override
	public void deleteProfitRuleByAgentId(String agentId) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(agentId!=null &&!"".equals(agentId)){
				String sql = "delete from t_profit_rule p where p.agent_id='"+agentId+"'";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除代理商分润规则出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除代理商分润规则出错");
		}
	}

	@Override
	public void deleteAgentMer(long agentId, String accountNo) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if( !"".equals(String.valueOf(agentId))){
				String sql = "delete from T_OPERATORS where id = " + agentId;
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除代理商出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除代理商出错");
		}
	}
	
	@Override
	public List<ProfitPercent> queryProfitPer(String agentId) throws DaoException {
		List<ProfitPercent> profitRuleList=new LinkedList<ProfitPercent>();
		try {
			Session session = sessionFactory.getCurrentSession();
			StringBuffer sb=new StringBuffer("from ProfitPercent p where p.agent_id = ? order by p.id ");
			
			Query query=session.createQuery(sb.toString());
			query.setString(0, agentId);

			profitRuleList=query.list();
		}catch (Exception e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询代理商分润比例出错");
		}
		return profitRuleList;
	}

	@Override
	public void deleteProfitPerByAgentId(String agentId) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(agentId!=null &&!"".equals(agentId)){
				String sql = "delete from t_profit_percent p where p.agent_id='"+agentId+"'";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除代理商分润比例出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除代理商分润比例出错");
		}
	}
	@Override
	public int selectBlackInfoByType(String blackType,String blackNum) throws DaoException {
		int icount = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String sql=" select count(1) from APSTFR.TBL_POS_BLACK_INF t where t.in_flag = '1' and t.black_type = :blackType and t.black_num = :blackNum ";
			SQLQuery query = session.createSQLQuery(sql);
			query.setParameter("blackType", blackType);
			query.setParameter("blackNum", blackNum);
			Object obj=query.uniqueResult();
			if(obj!=null){
				icount=Integer.valueOf(obj.toString());// 手机号/身份证/卡号 黑名单
			}
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询" + TblBlackInfType.getName(blackType) + "-黑名单信息出错");
		}
		return icount;
	}
}
